<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="通知创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="通知创建时间"></el-input>
    </el-form-item>
    <el-form-item label="源用户id" prop="sourceUserId">
      <el-input v-model="dataForm.sourceUserId" placeholder="源用户id"></el-input>
    </el-form-item>
    <el-form-item label="目标用户id" prop="destUserId">
      <el-input v-model="dataForm.destUserId" placeholder="目标用户id"></el-input>
    </el-form-item>
    <el-form-item label="目标地址id" prop="destArticleId">
      <el-input v-model="dataForm.destArticleId" placeholder="目标地址id"></el-input>
    </el-form-item>
    <el-form-item label="通知标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="通知标题"></el-input>
    </el-form-item>
    <el-form-item label="通知内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="通知内容"></el-input>
    </el-form-item>
    <el-form-item label="通知状态:0-未读;1-已读;2-删除" prop="status">
      <el-input v-model="dataForm.status" placeholder="通知状态:0-未读;1-已读;2-删除"></el-input>
    </el-form-item>
    <el-form-item label="通知类型:0-系统通知;1-@消息;2-回复消息;3-点赞消息" prop="notifyType">
      <el-input v-model="dataForm.notifyType" placeholder="通知类型:0-系统通知;1-@消息;2-回复消息;3-点赞消息"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createTime: '',
          sourceUserId: '',
          destUserId: '',
          destArticleId: '',
          title: '',
          content: '',
          status: '',
          notifyType: ''
        },
        dataRule: {
          createTime: [
            { required: true, message: '通知创建时间不能为空', trigger: 'blur' }
          ],
          sourceUserId: [
            { required: true, message: '源用户id不能为空', trigger: 'blur' }
          ],
          destUserId: [
            { required: true, message: '目标用户id不能为空', trigger: 'blur' }
          ],
          destArticleId: [
            { required: true, message: '目标地址id不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '通知标题不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '通知内容不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '通知状态:0-未读;1-已读;2-删除不能为空', trigger: 'blur' }
          ],
          notifyType: [
            { required: true, message: '通知类型:0-系统通知;1-@消息;2-回复消息;3-点赞消息不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/main/notify/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createTime = data.notify.createTime
                this.dataForm.sourceUserId = data.notify.sourceUserId
                this.dataForm.destUserId = data.notify.destUserId
                this.dataForm.destArticleId = data.notify.destArticleId
                this.dataForm.title = data.notify.title
                this.dataForm.content = data.notify.content
                this.dataForm.status = data.notify.status
                this.dataForm.notifyType = data.notify.notifyType
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/main/notify/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createTime': this.dataForm.createTime,
                'sourceUserId': this.dataForm.sourceUserId,
                'destUserId': this.dataForm.destUserId,
                'destArticleId': this.dataForm.destArticleId,
                'title': this.dataForm.title,
                'content': this.dataForm.content,
                'status': this.dataForm.status,
                'notifyType': this.dataForm.notifyType
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
